<?php

namespace app\api\model;

use think\Model;

/**
 * 房间音频配置模型
 */
class RoomAudioConfig extends Model
{
    protected $name = 'api_room_audio_configs';
    
    // 设置字段信息
    protected $schema = [
        'id'            => 'int',
        'room_id'       => 'int',
        'category_type' => 'string',
        'play_mode'     => 'string',
        'interval_min'  => 'int',
        'interval_max'  => 'int',
        'auto_play'     => 'int',
        'volume'        => 'decimal',
        'is_expanded'   => 'int',
        'status'        => 'int',
        'create_time'   => 'bigint',
        'update_time'   => 'bigint',
    ];

    // 自动时间戳（毫秒）
    protected $autoWriteTimestamp = false;

    // 状态常量
    const STATUS_NORMAL = 1;  // 正常
    const STATUS_DISABLED = 0; // 禁用

    // 分类类型常量
    const CATEGORY_SCENE = 'scene';        // 场景音频
    const CATEGORY_BACKGROUND = 'background'; // 背景音乐
    const CATEGORY_OTHER = 'other';        // 其他音频

    // 播放模式常量
    const PLAY_MODE_SEQUENTIAL = 'sequential'; // 顺序播放
    const PLAY_MODE_RANDOM = 'random';        // 随机播放
    const PLAY_MODE_LOOP = 'loop';            // 循环播放
    const PLAY_MODE_MANUAL = 'manual';        // 手动播放

    /**
     * 获取分类类型列表
     */
    public static function getCategoryTypes()
    {
        return [
            self::CATEGORY_SCENE => '场景音频',
            self::CATEGORY_BACKGROUND => '背景音乐',
            self::CATEGORY_OTHER => '其他音频',
        ];
    }

    /**
     * 获取播放模式列表
     */
    public static function getPlayModes()
    {
        return [
            self::PLAY_MODE_SEQUENTIAL => '顺序播放',
            self::PLAY_MODE_RANDOM => '随机播放',
            self::PLAY_MODE_LOOP => '循环播放',
            self::PLAY_MODE_MANUAL => '手动播放',
        ];
    }

    /**
     * 获取房间的配置
     */
    public static function getRoomConfigs($roomId)
    {
        return self::where('room_id', $roomId)
            ->where('status', self::STATUS_NORMAL)
            ->order('category_type')
            ->select();
    }

    /**
     * 创建默认配置
     */
    public static function createDefaultConfigs($roomId)
    {
        $timestamp = time() * 1000;
        
        $defaultConfigs = [
            [
                'room_id' => $roomId,
                'category_type' => self::CATEGORY_SCENE,
                'play_mode' => self::PLAY_MODE_SEQUENTIAL,
                'interval_min' => 20,
                'interval_max' => 40,
                'auto_play' => 1,
                'volume' => 1.00,
                'is_expanded' => 1,
                'status' => self::STATUS_NORMAL,
                'create_time' => $timestamp,
                'update_time' => $timestamp,
            ],
            [
                'room_id' => $roomId,
                'category_type' => self::CATEGORY_BACKGROUND,
                'play_mode' => self::PLAY_MODE_LOOP,
                'interval_min' => 0,
                'interval_max' => 0,
                'auto_play' => 0,
                'volume' => 0.30,
                'is_expanded' => 1,
                'status' => self::STATUS_NORMAL,
                'create_time' => $timestamp,
                'update_time' => $timestamp,
            ],
            [
                'room_id' => $roomId,
                'category_type' => self::CATEGORY_OTHER,
                'play_mode' => self::PLAY_MODE_MANUAL,
                'interval_min' => 5,
                'interval_max' => 15,
                'auto_play' => 0,
                'volume' => 1.00,
                'is_expanded' => 1,
                'status' => self::STATUS_NORMAL,
                'create_time' => $timestamp,
                'update_time' => $timestamp,
            ]
        ];

        return self::insertAll($defaultConfigs);
    }

    /**
     * 更新配置
     */
    public function updateConfig($data)
    {
        $updateData = [
            'update_time' => time() * 1000,
        ];

        // 安全地更新字段
        $allowedFields = ['play_mode', 'interval_min', 'interval_max', 'auto_play', 'volume', 'is_expanded'];
        foreach ($allowedFields as $field) {
            if (isset($data[$field])) {
                $updateData[$field] = $data[$field];
            }
        }

        return $this->save($updateData);
    }
}
